package _.javax.mail.internet.util;

import java.util.Arrays;
import java.util.List;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.budo.support.lang.util.StringUtil;

/**
 * @author limingwei
 */
public class InternetAddressUtil {
    public static List<InternetAddress> toAddressList(String address) {
        InternetAddress[] addressArray = toAddressArray(address);
        return null == addressArray ? null : Arrays.asList(addressArray);
    }

    public static InternetAddress[] toAddressArray(String address) {
        InternetAddress internetAddress = toAddress(address);
        return null == internetAddress ? null : new InternetAddress[] { internetAddress };
    }

    public static InternetAddress toAddress(String address) {
        try {
            return StringUtil.isEmpty(address) ? null : new InternetAddress(address);
        } catch (AddressException e) {
            throw new RuntimeException(e);
        }
    }
}